﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.codeplex.eatoutadvocate.Engine;
using System.Configuration;
namespace com.codeplex.eatoutadvocate.DAL
{
    public class EatOutInfo
    {
        /// <summary>
        /// DB Connection String
        /// </summary>
        private static string ConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["EatOutAdvocateData"].ConnectionString;
            }
        }

        private EatOutInfo(){ /* Should never be initialized */ }

        /// <summary>
        /// Gets Eat out objects for a given user
        /// </summary>
        /// <param name="userID">User ID</param>
        /// <returns>List of Eat Out objects</returns>
        public static List<Engine.EatOut> get(int userID)
        {
            List<Engine.EatOut> bag = new List<Engine.EatOut>();
            //Get connection string
            string connStr = ConnectionString;
            //Establish Data connection
            EatOutAdvocateDataContext data = new EatOutAdvocateDataContext(connStr);
           
            var eatouts = from eo in data.EatOuts
                            where eo.UserID == userID
                            select eo;

            foreach (EatOut eatout in eatouts)
            {
                /*
                    *  Time
                    *  Mood
                    *  MoodLevel
                    *  Weather
                    *  Temperature
                    *  Restaurant
                    */
                int moodLevel;

                if (eatout.MoodLevel != null)
                {
                    moodLevel = (int)eatout.MoodLevel;
                }
                else
                {
                    moodLevel = 0;
                }

                bag.Add(new Engine.EatOut.Builder()
                    .Time((int)eatout.TimeHour)
                    .Mood((Engine.Mood)eatout.Mood)
                    .MoodLevel(moodLevel)
                    .Weather((Engine.Weather)eatout.Weather)
                    .Temperature((Engine.Temperature)eatout.Temperature)
                    .Restaurant(eatout.Restaurant.Name)
                    .Build());
            }
            return bag;
        }
    }
}
